package ex8;

import java.util.ArrayList;
import java.util.Scanner;


interface GeneralStack<E> {
	E push(E e);            
	E pop();                 
	E peek();                
	boolean empty();
	int size();     
}


class ArrayListGeneralStack<E> implements GeneralStack<E> {
	
	private ArrayList<E> list;
	
	public ArrayListGeneralStack() {
		list = new ArrayList<E>();
	}
	
	@Override
	public E push(E e) {
		if (e == null) {
			return null;
		} else {
			list.add(e);
			return e;
		}
	}

	@Override
	public E pop() {
		if (list.isEmpty()) {
			return null;
		} else {
			return list.remove(list.size() - 1);
		}
	}

	@Override
	public E peek() {
		if (list.isEmpty()) {
			return null;
		} else {
			return list.get(list.size() - 1);
		}
	}

	@Override
	public boolean empty() {
		// TODO Auto-generated method stub
		return list.isEmpty();
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public String toString() {
		return list.toString();
	}
	
}


class Car {
	private int id; 
	private String name;
	
	public Car(int id, String name) {
		this.id = id;
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Car [id=" + id + ", name=" + name + "]";
	}
}


public class Test3 {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		
		while (true) {
			String string = scanner.nextLine();
			if (string.equals("Integer")) {
				System.out.println("Integer Test");
				ArrayListGeneralStack<Integer> arrayListGeneralStack = new ArrayListGeneralStack<Integer>();
				int m = scanner.nextInt();
				int n = scanner.nextInt();
				for (int i = 0; i < m; i++) {
					System.out.println("push:" + arrayListGeneralStack.push(scanner.nextInt()));
				}
				for (int i = 0; i < n; i++) {
					System.out.println("pop:" + arrayListGeneralStack.pop());
				}
				
				System.out.println(arrayListGeneralStack);
				int sum = 0;
				while (!arrayListGeneralStack.empty()) {
					sum += arrayListGeneralStack.pop();
				}
				System.out.println("sum=" + sum);
				System.out.println(arrayListGeneralStack.getClass().getInterfaces()[0]);
			} else if (string.equals("Double")) {
				System.out.println("Double Test");
				ArrayListGeneralStack<Double> arrayListGeneralStack = new ArrayListGeneralStack<Double>();
				int m = scanner.nextInt();
				int n = scanner.nextInt();
				for (int i = 0; i < m; i++) {
					System.out.println("push:" + arrayListGeneralStack.push(scanner.nextDouble()));
				}
				for (int i = 0; i < n; i++) {
					System.out.println("pop:" + arrayListGeneralStack.pop());
				}
				
				System.out.println(arrayListGeneralStack);
				double sum = 0;
				while (!arrayListGeneralStack.empty()) {
					sum += arrayListGeneralStack.pop();
				}
				System.out.println("sum=" + sum);
				System.out.println(arrayListGeneralStack.getClass().getInterfaces()[0]);
			} else if (string.equals("Car")) {
				System.out.println("Car Test");
				ArrayListGeneralStack<Car> arrayListGeneralStack = new ArrayListGeneralStack<Car>();
				int m = scanner.nextInt();
				int n = scanner.nextInt();
				for (int i = 0; i < m; i++) {
					System.out.println("push:" + arrayListGeneralStack.push(new Car(scanner.nextInt(), scanner.next())));
				}
				for (int i = 0; i < n; i++) {
					System.out.println("pop:" + arrayListGeneralStack.pop());
				}
				
				System.out.println(arrayListGeneralStack);
				while (!arrayListGeneralStack.empty()) {
					Car car = arrayListGeneralStack.pop();
					System.out.println(car.getName());
				}
				
				System.out.println(arrayListGeneralStack.getClass().getInterfaces()[0]);
			} else if (string.equals("quit")) {
				break;
			}
			scanner.nextLine();
		}
		scanner.close();
	}
}